package com.example.pda.ui.main;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.example.pda.R;
import com.example.pda.adapter.CardAdapter;
import com.example.pda.common.BaseActivity;
import com.example.pda.common.CommonCallback;
import com.example.pda.common.DaoUtils;
import com.example.pda.common.DialogUtils;
import com.example.pda.common.SharedUtils;
import com.example.pda.common.Utils;
import com.example.pda.data.model.MainCard;
import com.example.pda.databinding.ActivityMainBinding;
import com.example.pda.net.HttpManager;
import com.example.pda.ui.list.OrderListActivity;
import com.example.pda.ui.login.LoginActivity;
import com.example.pda.ui.login.SettingActivity;
import com.example.pda.ui.query.QueryActivity;
import com.example.pda.ui.query.QueryStoreActivity;
import com.example.pda.ui.stock.StockActivity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

public class MainActivity extends BaseActivity implements CommonCallback<MainCard> {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setLifecycleOwner(this);
        MainViewModel mainViewModel = new ViewModelProvider(this, new MainViewModelFactory())
                .get(MainViewModel.class);

        CardAdapter adapter = new CardAdapter();
        adapter.setListener(this);
        binding.mainRecycler.setLayoutManager(new GridLayoutManager(this, 2));
        binding.mainRecycler.addItemDecoration(new MyItemDecoration(10));
        binding.mainRecycler.setAdapter(adapter);
        mainViewModel.getCardResult().observe(this, adapter::setData);
        mainViewModel.loadMainCards();

    }

    @Override
    public void onResult(MainCard data, Object... objects) {
        if (TextUtils.isEmpty(SharedUtils.getUserName()) && !data.getText().equals("系统设置")) {
            DialogUtils.showInfoDialog(this, "请登陆", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    DialogUtils.closeInfoDialog();
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent);

                }
            }, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    DialogUtils.closeInfoDialog();
                }
            });
            return;
        }
        switch (data.getText()) {
            case "采购入库":
            case "材料出库":
            case "调拨出库":
                OrderListActivity.start(MainActivity.this, data.getType(), data.getText());
                break;
            case "采购订单查询":
                QueryActivity.start(MainActivity.this, data.getType());
                break;
            case "实时库存":
                QueryStoreActivity.start(MainActivity.this, data.getType());
                break;
            case "清除缓存":
                DialogUtils.showInfoDialog(this, "是否清除缓存", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DialogUtils.closeInfoDialog();
                        DaoUtils.getsUtils().reset();
                        Utils.getInstance().showToast("清除成功");
                    }
                });
                break;
            case "系统设置":
                Intent intent = new Intent(MainActivity.this, SettingActivity.class);
                startActivity(intent);
                break;
            case "退出":
                SharedUtils.setUserName("");
                break;
            default:
                break;
        }
    }
}
